package com.反射Reflection.HomeWork;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class homeWork01 {
    /**
     *
     * @param args
     * 1.反射创建对象
     * 2.得到属性并改称hello
     * 3.得到getName方法并调用输出
     */
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {

        Class<?> cls = Class.forName("com.反射Reflection.HomeWork.PrivateTest");
        /// 创建对象
        Object privateTest01 = cls.getDeclaredConstructor().newInstance();
        ///得到属性并改成hello
        Field name = cls.getDeclaredField("name");
        name.setAccessible(true);
        name.set(null,"hello");

        /// 得到方法，并调用
        Method getName = cls.getDeclaredMethod("getName");
        System.out.println(getName.invoke(null));
    }
}


class PrivateTest{
    private static String name = "hello kitty";

    public static String getName() {
        return name;
    }
}